package com.twosnail.frame.controller;

import com.jfinal.core.Controller;
import com.jfinal.log.Logger;
import com.twosnail.frame.commin.util.result.ResultObj;
import com.twosnail.frame.model.WxUserAccoutLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.math.BigDecimal;

/**
 * Created by yangcs on 2017/8/14.
 */
public class TxController extends Controller {
    private Logger logger = Logger.getLogger(TxController.class) ;

    @RequiresPermissions("TxController")
    public void index(){
        Integer pageNum = getParaToInt( "pageNum" ) ;
        Integer numPerPage = getParaToInt( "numPerPage" ) ;
        pageNum = pageNum == null ? 1 : pageNum;
        numPerPage = ( numPerPage == null || numPerPage == 0 ) ? 5 : numPerPage;
        try {
            setAttr( "list", WxUserAccoutLog.me.getTxList(pageNum,numPerPage));
        } catch (Exception e) {
            this.logger.warn( "提现列表信息初始化失败！" , e );
        }
        render( "tx_list.html" );
    }

    /**
     * 提现
     */
    public void tx(){
        Long logId  = getParaToLong( "logId" ) ;
        Double accout = Double.parseDouble(getPara("accout"));
        Long userId = getParaToLong( "wxUserId" ) ;
        try {
            if( accout == null || logId == null || userId == null ) {
                renderJson( new ResultObj( ResultObj.FAIL , "参数不能为空！" , null ) );
            } else {
                boolean result = WxUserAccoutLog.me.tx(logId,accout,userId);
                if(result){
                    renderJson( new ResultObj( ResultObj.SUCCESS , "操作成功" , null ) );
                }else{
                    renderJson( new ResultObj( ResultObj.FAIL , "操作失败！" , null ) );
                }
            }
        } catch ( Exception e ) {
            this.logger.info( "系统异常，添加代金劵失败！", e );
            renderJson( new ResultObj( ResultObj.FAIL , e.getMessage() , null ) );
        }
    }
}
